g = [7, 5] ;
trel = poly2trellis(2, g);

total = 1000000;
msgLen = 10;

channelBERs = logspace(-1,-4,7);
msgBER = zeros(channelBERs);

for i = 1:length(channelBERs)
  channelBER = channelBERs(i);
  errCount = 0;
  for j = 1:total/msgLen
    msg = round(rand(msgLen, 1));
    code = convenc(msg, trel);
    code = dec2bin(code, 2);
    errPos = find(rand(msgLen, 1) < channelBER);
    rcv_code = code;
    rcv_code(errPos) = ~code(errPos);
    rcv_code = bin2dec(rcv_code);
    decodeMsg = convdec(rcv_code, trel);
    errCount = errCount + length(find(msg ~= decodeMsg));
  end
  msgBER(i) = errCount/total;
end


